<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            user-select: none;
        }

        .right {
            font-size: 12px;
            color: green;
        }

        .err {
            font-size: 12px;
            color: red;
        }
    </style>
</head>

<body>
    <form onsubmit="return false">
        <p>
            <label for="user">用户名:</label>
            <input type="text" id="user" placeholder="用户名">
            <span id="user_span"></span>
        </p>
        <p>
            <label for="">密&emsp;码:</label>
            <input type="text" id="pwd">
            <span id="pwd_span"></span>
        </p>
        <p>
            <input type="submit" id="btn" value="登录">
        </p>
    </form>
</body>

<script>

    // 1. 获取元素 
    var userInp = document.getElementById("user");
    var userSpan = document.getElementById("user_span");

    var pwdInp = document.getElementById("pwd");
    var pwdSpan = document.getElementById("pwd_span");

    userInp.onblur = function () {
        var user = userInp.value.trim();
        userInp.value = user;
        isUserOk = false; // 每次验证 默认还原为false

        var reg = /^[a-zA-Z_]\w{5,11}$/;

        if (reg.test(user)) {
            userSpan.textContent = "√";
            userSpan.className = "right";
            isUserOk = true;
        } else {
            userSpan.textContent = "用户名由数字、字母、下划线组成,6-12位,且不能以数字开头";
            userSpan.className = "err";
        }
    }

    pwdInp.onblur = function () {
        var pwd = pwdInp.value.trim();
        pwdInp.value = pwd;

        isPwdOk = false;

        var reg = /^\w{6,12}$/;

        if (reg.test(pwd)) {

            var isExistNum = /[0-9]/.test(pwd);
            var isExistSmall = /[a-z]/.test(pwd);
            var isExistBig = /[A-Z]/.test(pwd);
            var isExistSpe = /[_]/.test(pwd);
            var level = isExistNum + isExistSmall + isExistBig + isExistSpe;

            pwdSpan.textContent = "密码强度:" + level;
            pwdSpan.className = "right";
            isPwdOk = true;

        } else {
            pwdSpan.textContent = "密码由数字、字母、下划线组成,6-12位";
            pwdSpan.className = "err";
        }
    }

    btn.onclick = function(){
        var a = getCookie("user");
        var users = JSON.parse(a);

        var b = getCookie("pwd");
        var pwds = JSON.parse(b);

        if(users == userInp.value){
            if(pwds == pwdInp.value){
                location.href = "http://127.0.0.1:5500/js/day43/cookie%E4%BD%9C%E4%B8%9A.html/index.html"
            }else{
                pwdSpan.textContent = "密码错误";
                pwdSpan.className = "err";
            }
        }else{
            userSpan.textContent = "用户名错误";
            userSpan.className = "err";
        }
    }

    function getCookie(key){
        var cookie = document.cookie;
        if(cookie){
            var list = cookie.split("; ");//;后面有个空格*************
            for(var i = 0 ; i < list.length ; i++){
                var item = list[i];
                var attr = item.split("=")[0];
                var val = item.split("=")[1];
                if(key === attr){
                    return val;
                }
            }
        }
        return "";
    }
</script>

</html>